# -*- coding: utf-8 -*-
# @Time : 2023/12/18 16:34
# @Author : Z
# @Email : 1473858993@qq.com
# @File : yaml_util.py
# @Project : apitwo
import os

import yaml

from common.dir import yml_dir, api_yaml_dir


class YamlUtil:
    # 读取extract.yml文件
    def read_extract_yaml(self):
        filepath = os.path.join(yml_dir, 'extract.yml')
        with open(filepath, mode='r', encoding='utf-8') as f:
            value = yaml.load(stream=f, Loader=yaml.FullLoader)
            return value

    # 写入extract_yaml文件
    def write_extract_yaml(self, data):
        filepath = os.path.join(yml_dir, 'extract.yml')
        # 对应文件写入内容        a：进行追加
        #                      w：写入但会覆盖
        with open(filepath, mode='a', encoding='utf-8') as f:
            yaml.dump(data=data, stream=f, allow_unicode=True)

    # 清除extract_yaml文件
    def clear_extract_yaml(self):
        filepath = os.path.join(yml_dir, 'extract.yml')
        with open(filepath, mode='w', encoding='utf-8') as f:
            f.truncate()

    # 清除extract_yaml文件，在第一种文件清空不允许的情况下使用此方法，逻辑为重置为空
    # def clean_extract_yaml(self):
    #     filepath = os.path.join(yml_dir, 'extract.yml')
    #     with open(filepath, mode='w', encoding='utf-8') as f:
    #         f.write('')

    # 读取接口配置yml文件
    def read_api_yaml(self, filename):
        filepath = os.path.join(api_yaml_dir, filename)
        with open(filepath, mode='r', encoding='utf-8') as f:
            value = yaml.load(stream=f, Loader=yaml.FullLoader)
            return value



yaml_urils = YamlUtil()